package com.iot08.admin.service;

import com.iot08.admin.service.page.PageVO;
import com.iot08.admin.service.page.PageableImpl;
import com.iot08.common.entity.admin.SysUser;
import com.iot08.common.entity.app.Advert;
import com.iot08.common.entity.app.Channels;
import com.iot08.common.response.Response;
import com.iot08.common.utils.RandomUtil;
import com.iot08.common.utils.StringUtil;
import org.bson.types.ObjectId;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @version 1.0
 * @Description:
 * @time: 2020/10/23 16:47
 * @author: Rock
 */
@Service
public class AppChannelService {

    @Resource(name = "appMongoTemplate")
    private MongoTemplate mongoTemplate;

    @Resource
    private OperationService operationService;
    @Resource
    private VersionUpdatesService versionUpdatesService;

    /**
     * @param page
     * @param user
     * @time: 2020/10/23 17:00
     * @author: Rock
     * @version 1.0
     */
    public Page<Channels> list(PageVO page, SysUser user) {
        PageableImpl pageable = new PageableImpl();
        page.setSort(Sort.by(Sort.Order.desc("createtime")));
        pageable.setPage(page);
        Criteria criteria = new Criteria();
        Query query = new Query().addCriteria(criteria);
        long count = mongoTemplate.count(query, Advert.class);
        page.setTotalResult((int) count);
        List<Channels> list = mongoTemplate.find(query.with(pageable), Channels.class);
        Page<Channels> result = new PageImpl<Channels>(list, pageable, count);
        return result;
    }

    /**
     * @param id
     * @time: 2020/10/23 18:18
     * @author: Rock
     * @version 1.0
     */
    public Channels getById(String id) {
        if (ObjectId.isValid(id)) {
            return mongoTemplate.findOne(new Query(Criteria.where("_id").is(new ObjectId(id))), Channels.class);
        }
        return null;
    }

    /**
     * @param shortName
     * @time: 2020/10/26 11:27
     * @author: Rock
     * @version 1.0
     */
    public Channels getByShortName(String shortName) {
        return mongoTemplate.findOne(new Query(Criteria.where("short_name").is(shortName)), Channels.class);
    }

    /**
     * @param channel
     * @time: 2020/10/23 18:15
     * @author: Rock
     * @version 1.0
     */
    public void add(Channels channel, SysUser user, String ip) {
        if (channel != null) {
            channel.setCreater(user.getAccount());
            channel.setChannelid(RandomUtil.getRandomStr(6));
            channel.setCreatetime(new Date());
            mongoTemplate.save(channel);
            operationService.add(user, ip, "渠道管理", "添加",
                    String.format("添加运营渠道: %s ", channel.getName()));
        }
    }

    /**
     * @param channel
     * @param user
     * @param ip
     * @time: 2020/10/23 18:28
     * @author: Rock
     * @version 1.0
     */
    public void update(Channels channel, SysUser user, String ip) {
        if (ObjectId.isValid(channel.getId())) {
            Query query = new Query(Criteria.where("_id").is(new ObjectId(channel.getId())));
            Update update = new Update();
            update.set("name", channel.getName());
            update.set("modifytime", new Date());
            mongoTemplate.updateFirst(query, update, Channels.class);
            operationService.add(user, ip, "渠道管理", "编辑",
                    String.format("修改运营渠道商: %s ", channel.getName()));
        }
    }

    /**
     * @param id
     * @param user
     * @param ip
     * @time: 2020/10/23 18:43
     * @author: Rock
     * @version 1.0
     */
    public Response<Object> del(String id, SysUser user, String ip) {
        if (ObjectId.isValid(id)) {
            Query query = new Query(Criteria.where("_id").is(new ObjectId(id)));
            Channels channel = mongoTemplate.findOne(query, Channels.class);
            if (channel != null) {
                if (versionUpdatesService.existField(channel.getShortName())) {
                    return Response.failedResponse("渠道被引用，无法删除");
                }
                String name = channel.getName();
                mongoTemplate.remove(query, Channels.class);
                operationService.add(user, ip, "渠道管理", "删除",
                        String.format("删除运营渠道商: %s ", name));
                return Response.successResponse();
            }
        }
        return Response.failedResponse();
    }

    /**
     * @param
     * @time: 2020/10/26 15:24
     * @author: Rock
     * @version 1.0
     */
    public List<Channels> listAll() {
        return mongoTemplate.find(new Query().with(Sort.by(Sort.Order.asc("createtime"))), Channels.class);
    }
}
